package com.mall.db.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.PmsProductMapper;
import com.mall.pojo.PmsProduct;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class PmsProductApiAmpl implements PmsProductApi {

    @Autowired
    private PmsProductMapper pmsProductMapper;

    //查询商品
    @Override
    public Page<PmsProduct> list(Integer brandId, String keyword, Integer pageNum, Integer pageSize, Integer productCategoryId, String productSn, Integer publishStatus, Integer verifyStatus) {
        Page<PmsProduct> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<PmsProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(brandId != null, PmsProduct::getBrandId, brandId);
        wrapper.like(keyword != null, PmsProduct::getName, keyword);
        wrapper.eq(productCategoryId != null, PmsProduct::getProductAttributeCategoryId, productCategoryId);
        wrapper.eq(productSn != null, PmsProduct::getProductSn, productSn);
        wrapper.eq(publishStatus != null, PmsProduct::getPublishStatus, publishStatus);
        wrapper.eq(verifyStatus != null, PmsProduct::getVerifyStatus, verifyStatus);
        return pmsProductMapper.selectPage(page, wrapper);
    }

    //根据商品名称或货号模糊查询
    @Override
    public List<PmsProduct> simpleList(String keyword) {
        LambdaQueryWrapper<PmsProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(keyword != null, PmsProduct::getName, keyword)
                .or()
                .like(keyword != null, PmsProduct::getProductSn, keyword);
        return pmsProductMapper.selectList(wrapper);
    }

    //批量修改删除状态
    @Override
    public void deleteStatus(List<Integer> ids, Integer deleteStatus) {
        LambdaQueryWrapper<PmsProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PmsProduct::getId, ids);
        PmsProduct pmsProduct = new PmsProduct();
        pmsProduct.setDeleteStatus(deleteStatus);
        pmsProductMapper.update(pmsProduct, wrapper);
    }

    //批量设为新品
    @Override
    public void newStatus(List<Integer> ids, Integer newStatus) {
        LambdaQueryWrapper<PmsProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PmsProduct::getId, ids);
        PmsProduct pmsProduct = new PmsProduct();
        pmsProduct.setNewStatus(newStatus);
        pmsProductMapper.update(pmsProduct, wrapper);
    }

    //批量上下架商品
    @Override
    public void publishStatus(List<Integer> ids, Integer publishStatus) {
        LambdaQueryWrapper<PmsProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PmsProduct::getId, ids);
        PmsProduct pmsProduct = new PmsProduct();
        pmsProduct.setPublishStatus(publishStatus);
        pmsProductMapper.update(pmsProduct, wrapper);
    }

    //批量推荐商品
    @Override
    public void recommendStatus(List<Integer> ids, Integer recommendStatus) {
        LambdaQueryWrapper<PmsProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PmsProduct::getId, ids);
        PmsProduct pmsProduct = new PmsProduct();
        pmsProduct.setRecommandStatus(recommendStatus);
        pmsProductMapper.update(pmsProduct, wrapper);
    }

    //批量修改审核状态
    @Override
    public void verifyStatus(List<Integer> ids, String detail, Integer verifyStatus) {
        LambdaQueryWrapper<PmsProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PmsProduct::getId, ids);
        PmsProduct pmsProduct = new PmsProduct();
        pmsProduct.setVerifyStatus(verifyStatus);
        pmsProductMapper.update(pmsProduct, wrapper);
    }

    //创建商品(八接口之一)
    @Override
    public void create(PmsProduct pmsProduct) {
        pmsProductMapper.insert(pmsProduct);
    }

    //更新商品 更新八张表之一
    @Override
    public void update(Integer id, PmsProduct pmsProduct) {
        LambdaQueryWrapper<PmsProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProduct::getId, id);
        pmsProductMapper.update(pmsProduct, wrapper);
    }


    @Override
    public PmsProduct selectById(Integer id) {
        return pmsProductMapper.selectById(id);
    }

    /**
     * 根据商品id查询商品信息
     *
     * @param productId
     * @return
     */
    @Override
    public PmsProduct findById(Long productId) {
        return pmsProductMapper.selectById(productId);
    }
}
